package com.noodles.mall.controller;


import com.noodles.mall.entity.Item;
import com.noodles.mall.service.ItemService;
import com.noodles.mall.util.Upload;
import com.noodles.mall.vo.PageVo;
import com.noodles.mall.web.JsonResult;
import com.noodles.mall.web.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/** 商品 **/
@RestController
@RequestMapping("/item")
public class ItemController {
    private static String dir="item";

    @Autowired
    Upload upload;

    @Autowired
    private ItemService itemService;

    /** 增加 **/
    @RequestMapping("/insert")
    public JsonResult insertItem(@RequestBody Item item) {
        System.out.println("ItemController: insert");
        System.out.println("item: "+item);
        return itemService.insert(item)
                ? JsonResult.success(item) : JsonResult.error(State.ERR_SQL);
    }

    /** 删除:  **/
    @DeleteMapping("/delete")
    public JsonResult delete(@RequestBody List<Integer> idList) {
        System.out.println("ItemController: delete");
        System.out.println("idList: "+idList);
        return itemService.delete(idList)
                ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 修改: **/
    @RequestMapping("/update")
    public JsonResult update(@RequestBody Item item) {
        System.out.println("ItemController: update");
        System.out.println("item: "+item);
        return itemService.update(item)
                ? JsonResult.success() : JsonResult.error(State.ERR_SQL);
    }

    /** 上传图片: 文件, 商品id **/
    @RequestMapping("/upload/{id}")
    public JsonResult upload(@RequestBody MultipartFile file,
                             @PathVariable(value = "id") Integer id) {
        System.out.println("ItemController: update");
        System.out.println("file: "+file);
        System.out.println("id: "+id);
        String imageUrl=upload.save(file,dir+"/"+id);
        return itemService.updateUrl(id,imageUrl)
                ? JsonResult.success(imageUrl) : JsonResult.error(State.ERR_SQL);
    }


    /** 查询单个: id **/
    @RequestMapping("/selectById/{id}")
    public JsonResult selectById(@PathVariable(value = "id") Integer id) {
        System.out.println("ItemController: selectItemDetails");
        System.out.println("id: "+id);
        Item item = itemService.selectOne(id);
        return JsonResult.success(item);
    }

    /** 分页查询: 无 **/
    @RequestMapping("/selectPage/{current}/{size}")
    public JsonResult selectPage(@PathVariable(value = "current") Long current,
                                 @PathVariable(value = "size") Long size) {
        System.out.println("ItemController: selectItemWithType");
        System.out.println("current: "+current);
        System.out.println("size: "+size);
        PageVo pageVo = itemService.selectPage(current, size);
        return JsonResult.success(pageVo);
    }

    /** 分页查询: 类别Id **/
    @RequestMapping("/selectPage/{current}/{size}/{typeId}")
    public JsonResult selectPage(@PathVariable(value = "current") Long current,
                                       @PathVariable(value = "size") Long size,
                                       @PathVariable(value = "typeId") Integer typeId) {
        System.out.println("ItemController: selectItemByType");
        System.out.println("current: "+current);
        System.out.println("size: "+size);
        System.out.println("typeId: "+typeId);
        PageVo pageVo = itemService.selectPage(current, size, typeId);
        return JsonResult.success(pageVo);
    }

    /** 分页查询: word(name,title,description) **/
    @RequestMapping("/selectPageByWord/{current}/{size}/{word}")
    public JsonResult selectPageByWord(@PathVariable(value = "current") Long current,
                                     @PathVariable(value = "size") Long size,
                                     @PathVariable(value = "word") String word) {
        System.out.println("ItemController: selectItemByType");
        System.out.println("current: "+current);
        System.out.println("size: "+size);
        System.out.println("word: "+word);
        PageVo pageVo = itemService.selectPage(current, size,word);
        return JsonResult.success(pageVo);
    }

    /** 查询商品总销量 **/
    @RequestMapping("/findSaleCount")
    public JsonResult findSaleCount() {
        System.out.println("ItemController: findSaleCount");
        Object num = itemService.selectSaleCount();
        return JsonResult.success(num);
    }

}
